package com.yu.config;

import org.apache.log4j.Logger;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.yu.model.Code;
import com.yu.model.Config;
import com.yu.model.Focus;
import com.yu.model.House;
import com.yu.model.Order;
import com.yu.model.User;
import com.yu.routes.AdminRoutes;
import com.yu.routes.FrontRoutes;


/**
 * 
 * createtime 上午9:57:31
 * 
 * @author chengjun
 */
public class DefaultConfig extends JFinalConfig {

	private static Logger logger = Logger.getLogger(DefaultConfig.class);

	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.properties");
		// PropKit.getBoolean("devMode")
		me.setDevMode(false);
		me.setViewType(ViewType.FREE_MARKER);
		// me.setBaseViewPath("/WEB-INF/view");
		me.setEncoding("utf-8");
		// me.setError404View("/WEB-INF/view/common/404.html");
		// me.setError500View("/WEB-INF/view/common/500.html");
	}

	@Override
	public void configRoute(Routes me) {
		// 不同路由 设置的basepath也不同
		me.add(new FrontRoutes()); // 前端 路由
		me.add(new AdminRoutes()); // 后端 路由
//		me.add("/", HomeController.class);
//		me.setBaseViewPath("/WEB-INF/view");
//		me.add("/admin", AdminController.class);
	}

	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbc.club.url"), PropKit.get("jdbc.club.username"),
				PropKit.get("jdbc.club.password"));
	}

	@Override
	public void configPlugin(Plugins me) {
		// 文件缓存
		/*
		{
			String pathEhcache = null;
			try {
				pathEhcache = PathKit.class.getClassLoader().getResource("ehcache.xml").toURI().getPath();
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
			me.add(new EhCachePlugin(pathEhcache));
		}
		*/
		DruidPlugin cp = createDruidPlugin();
		me.add(cp);
		/*
		C3p0Plugin cp = new C3p0Plugin(PropKit.get("jdbc.club.url"),
		PropKit.get("jdbc.club.username"),
		PropKit.get("jdbc.club.password"));
		me.add(cp);
		*/
		ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
		me.add(arp);
		
		arp.setShowSql(true);
		
		// 配置Mysql方言
		arp.setDialect(new MysqlDialect());
		// 配置属性名(字段名)大小写不敏感容器工厂
		arp.setContainerFactory(new CaseInsensitiveContainerFactory());
		arp.addMapping("w_user", "id", User.class);
		arp.addMapping("w_order", "id", Order.class);
		arp.addMapping("w_config", "id", Config.class);
		arp.addMapping("w_house", "id", House.class);
		arp.addMapping("w_code", "id", Code.class);
		arp.addMapping("w_focus", "id", Focus.class);
		// 注册用户
		/*
		arp.addMapping("club_manufacturer", "id", Manufacturer.class);
		arp.addMapping("club_order", "id", Order.class);
		arp.addMapping("club_face", "id", Face.class);
		arp.addMapping("club_prod", "id", Product.class);
		arp.addMapping("club_prod_detail", "id", ProductDetail.class);
		arp.addMapping("club_club", Club.class);
		arp.addMapping("club_users", "id", User.class);
		arp.addMapping("club_cashbalance", "id", Balance.class);
		arp.addMapping("club_cashlog", "id", Log.class);
		arp.addMapping("club_cashluckym", "id", LuckyMoney.class);
		arp.addMapping("club_cashluckymsnatch", "id", Snatch.class);
		arp.addMapping("club_userclub", "id", UserClub.class);
		arp.addMapping("club_cartype", "id", Cartype.class);
		arp.addMapping("club_cashwithdraw", "id", Withdraw.class);
		arp.addMapping("club_clubprod", "id", ClubProd.class);
		arp.addMapping("club_usercar", "id", UserCar.class);
		arp.addMapping("club_activity", "id", Activity.class);
		arp.addMapping("club_activitymark", "id", ActivityMark.class);
		arp.addMapping("club_tscity", "id", City.class);
		arp.addMapping("club_ask", "id", Ask.class);
		arp.addMapping("club_answer", "id", Answer.class);

		arp.addMapping("club_clubapplyhistory", "id", Apply.class);

		arp.addMapping("club_handbook", "id", Handbook.class);

		arp.addMapping("club_consumption", "id", Consumption.class);

		arp.addMapping("club_config", "id", Config.class);
		arp.addMapping("club_invitation", "id", Invitation.class);
		arp.addMapping("club_task", "id", Task.class);
		arp.addMapping("club_taskrecord", "id", Taskrecord.class);
		arp.addMapping("kart_flow", "id", Flow.class);
		arp.addMapping("kart_ticket", "id", Ticket.class);
		arp.addMapping("club_news", "id", News.class);
		*/
	}

	@Override
	public void configInterceptor(Interceptors me) {
		/*
		 * logger.info("配置全局拦截器开始.."); me.addGlobalActionInterceptor(new
		 * PhoneInterceptor()); me.addGlobalActionInterceptor(new
		 * LogInterceptor()); me.add(new
		 * TxByRegex("(.*save.*|.*update.*|.*delete.*)"));
		 * logger.info("配置全局拦截器结束..");
		 */
	}

	@Override
	public void afterJFinalStart() {
		FreeMarkerRender.getConfiguration().setClassicCompatible(true);
	}

	@Override
	public void configHandler(Handlers me) {
		// me.add(new ResourceHandler());
		me.add(new ContextPathHandler("base"));
	}

	@Override
	public void configEngine(Engine me) {
		me.addSharedFunction("/common/_layout.html");
		me.addSharedFunction("/common/_paginate.html");
	}
}
